﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RuoTeng.Util;

namespace RuoTeng.Windows.Forms
{
    public partial class MenuBox : RuoTengControl
    {       
        public MenuBox()
        {
            InitializeComponent();
     //添加软公告模块
            this.AddControl("notice",       new RuoTeng.Windows.Forms.Notice(),     17);
     //添加购买软件模块
            this.AddControl("buy",          new RuoTeng.Windows.Forms.Buy(),        18);
     //添加更新帮助模块
            this.AddControl("help_update",  new RuoTeng.Windows.Forms.HelpUpdate(), 19);
     //添加软件协议模块
            this.AddControl("use_protocol", new RuoTeng.Windows.Forms.UseProtocol(),20);
     //------------------------------------------------------------------------------------------------------
            //默认显示 软件公告模块
            RuoTengControl control= this.GetControl("notice");
            if (control != null)
            {  
                control.ShowPanel();
            //获取软件公告模块
                RuoTeng.Windows.Forms.Notice notice = (RuoTeng.Windows.Forms.Notice)control;
            //绑定 获取远程授权信息执行函数
                notice.ReponseServerFunc += new RuoTeng.Windows.Forms.Notice.ReponseServerHandle(this.Authorization);
            }
     //------------------------------------------------------------------------------------------------------
        }

        /// <summary>
        /// 授权回调函数,主要是回调给 上层 Mian 主界面对返回远程授权信息 处理
        /// </summary>
        /// <param name="auth">授权信息对象集合</param>
        public delegate void CallBackAuthorization(SortedList<string, object> auth);

        /// <summary>
        /// 回到函数指针
        /// </summary>
        public event CallBackAuthorization callback;

        /// <summary>
        /// MenuBox添加控件集合对象
        /// </summary>
        protected SortedList<string, RuoTengControl> _controls = new SortedList<string, RuoTengControl>();

        /// <summary>
        /// 返回控件对象集合
        /// </summary>
        public SortedList<string, RuoTengControl> ToolControls
        {
            get { return this._controls; }
        }

        /// <summary>
        /// 添加控件
        /// </summary>
        /// <param name="id">控件ID</param>
        /// <param name="control">控件实例对象</param>
        /// <param name="sort">控件排序</param>
        public void AddControl(string id, RuoTengControl control, int sort)
        {
            control.Sort = sort;
            this.AddControl(id,control);
        }

        /// <summary>
        /// 添加控件模块
        /// </summary>
        /// <param name="id">控件ID</param>
        /// <param name="control">控件实现对象</param>
        public void AddControl(string id,RuoTengControl control)
        {
            control.Dock    = DockStyle.Fill;  //添加的控件布局为填充布局
            control.Visible = false;           //控件不可见
            control.MenuBox = this;            //给控件关联MenuBox当前对象
        ///将控件添加到当前控件中
            this.panel_content.Controls.Add(control);

       // 将模块控件添加到MenuBox中控件集合 ,注意这里只是用于存放 模块控件对象引用指针
            this._controls.Add(id,control);
            if (control.ToolBar != null)
            {  
                control.ToolBar.Size = new System.Drawing.Size(64, 50);
            //将模块切换 菜单控件 pictureBox控件添加到MenuBox中
                this.Controls.Add(control.ToolBar);
           ///------------------------------------------------------------------------------------------------------
           //由于添加了新的控件,需要重新对菜单控件进行排序
           //遍历 所有的模块控件 获取他们指定的排序号, 放入sorts中,实现正序排列
                SortedList<int, RuoTengControl> sorts = new SortedList<int, RuoTengControl>();
                foreach (KeyValuePair<string, RuoTengControl> pari in this._controls)
                {
                    RuoTengControl __control = pari.Value;
                    int key=__control.Sort;
                    while (sorts.ContainsKey(key))
                    {
                        key++;
                    }
                    try
                    {
                        sorts.Add(key, __control);
                    }
                    catch { }
                } 
             ///-----------------------------------------------------------------------------------------------------
             ///对所有的菜单控件位置进行更新
                int X = 3; int Y = 2;
                this.SuspendLayout();
                foreach (KeyValuePair<int, RuoTengControl> pari in sorts)
                {
                    if (pari.Value.ToolBar != null)
                    {
                        pari.Value.ToolBar.Location = new System.Drawing.Point(X, Y);                       
                    }
                    X += 64;
                }
               this.ResumeLayout(true);
            ///-----------------------------------------------------------------------------------------------------
               this.Authorization(new SortedList<string, object>());
            }
        }
     
        /// <summary>
        /// 根据ID获取响应的控件对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RuoTengControl GetControl(string id)
        {
            return this._controls.ContainsKey(id) ? this._controls[id] : null;
        }
        
        /// <summary>
        ///  Menbox控件处理从服务获取的授权信息
        /// </summary>
        /// <param name="sort"></param>
        public override  void Authorization(SortedList<string, object> sort)
        {
            string  isend  = GetValue("isend","",sort);
            bool    isdemo = GetValue("isdemo",true, sort);

            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MenuBox));
         //检测是否需要显示VIP图片标志
            this.pictureBox_vip.Image = (isend ==MachineCode.GetNewMachinecode() + "can use") && !isdemo
                                      ?
                                      ((System.Drawing.Image)(resources.GetObject("pictureBox_vip.Image")))
                                      : 
                                      null;

        //遍历Menubox中其他控件,并调用相应的授权信息处理函数
            foreach (KeyValuePair<string, RuoTengControl> pari in this._controls)
            {
                pari.Value.Authorization(sort);
            }           
        }  
       
    /// <summary>
    ///  执行获取授权验证信息
    /// </summary>
        public void Authorization()
        {          
            try
            {
                new System.Threading.Thread(delegate()
                {
                    RuoTeng.Util.Auth.Authorization authorization = new RuoTeng.Util.Auth.Authorization();
                    //程序最新版本检测
                    if (!authorization.CheckUpdate())
                    {
                        MessageBox.Show(RuoTeng.Util.Auth.Authorization.UpdateError);
                    }
                    SortedList<string, object> sort = new SortedList<string, object>();
                    //程序授权信息检测
                    if (!authorization.DoAuthorization(out sort))
                    {
                        MessageBox.Show(RuoTeng.Util.Auth.Authorization.NotFoundServer);
                    }
                  //从服务器获取的授权信息,进行软件授权验证
                    this.Authorization(sort);
                    if (this.callback != null) {   this.callback(sort);  }
                }).Start();
            }
            catch
            {
                MessageBox.Show("程序无法连接服务器获取信息");
            }    
        }
    }
}
